package com.eastfair.venuebooking.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venuebooking.dao.ExhibitionBusinessOpportunityMapper;
import com.eastfair.venuebooking.entity.Exhibition;
import com.eastfair.venuebooking.entity.ExhibitionBusinessOpportunity;
import com.eastfair.venuebooking.service.ExhibitionBusinessOpportunityService;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.aop.framework.AopContext;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.annotation.annotation.cache.Cache;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.eastfair.venuebooking.vo.*;
import com.eastfair.venuebooking.dto.*;

/**
 * <p>
 * 业务实现类
 * 展会商机关系
 * </p>
 *
 * @author linan
 * @date 2023-04-03
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionBusinessOpportunityServiceImpl extends SuperServiceImpl<ExhibitionBusinessOpportunityMapper, ExhibitionBusinessOpportunity> implements ExhibitionBusinessOpportunityService {

    @Override
    protected R<Boolean> handlerSave(ExhibitionBusinessOpportunity model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        model.setTenantId(ContextUtil.getTenantId());
        //雪花ID
        return R.successDef();
    }


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionBusinessOpportunity> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<ExhibitionBusinessOpportunity> list(ExhibitionBusinessOpportunityDTO modelDTO, List<Long> ids) {
        return ((ExhibitionBusinessOpportunityService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'exhibitionId', #args[0]?.exhibitionId}," +
                            "{'businessOpportunityId', #args[0]?.businessOpportunityId}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<ExhibitionBusinessOpportunity> listOfCache(ExhibitionBusinessOpportunityDTO modelDTO, List<Long> ids) {
        //查询
        ExhibitionBusinessOpportunity queryModel = new ExhibitionBusinessOpportunity();
        if (modelDTO != null) {
            BeanUtils.copyProperties(modelDTO, queryModel);
        }
        QueryWrap<ExhibitionBusinessOpportunity> wrapper = Wraps.q(queryModel);
        if (ids != null && !ids.isEmpty()) {
            wrapper.lambda().in(ExhibitionBusinessOpportunity::getId, ids);
        }
        //添加其他业务查询条件
        wrapper.lambda().eq(ExhibitionBusinessOpportunity::getIsDeleted, BusinessConstant.DELETE_NO);
        List<ExhibitionBusinessOpportunity> modelList = list(wrapper);
        return modelList;
    }

    @Override
    public List<Long> queryBusinessOpportunityIds(Long id) {
        return baseMapper.queryBusinessOpportunityIds(id);
    }

    @Override
    public ExhibitionBusinessOpportunity getByBusinessOpportunityId(Long businessOpportunityId) {
        QueryWrapper<ExhibitionBusinessOpportunity> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ExhibitionBusinessOpportunity::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(businessOpportunityId != null, ExhibitionBusinessOpportunity::getBusinessOpportunityId, businessOpportunityId)
        ;
        List<ExhibitionBusinessOpportunity> list = list(queryWrapper);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public boolean existExhibition(List<Long> businessOpportunityIds, Long oldExhibition) {
        List<ExhibitionBusinessOpportunity> list = list(new QueryWrapper<ExhibitionBusinessOpportunity>().lambda()
                .in(ExhibitionBusinessOpportunity::getBusinessOpportunityId, businessOpportunityIds)
                .eq(ExhibitionBusinessOpportunity::getIsDeleted, BusinessConstant.DELETE_NO)
                .ne(Objects.nonNull(oldExhibition), ExhibitionBusinessOpportunity::getId,oldExhibition));
        return list.size() > 0;
    }

    @Override
    public void removeByExhibitionId(Long exhibitionId) {
        update(new UpdateWrapper<ExhibitionBusinessOpportunity>().lambda()
                .eq(ExhibitionBusinessOpportunity::getIsDeleted,BusinessConstant.DELETE_NO)
                .eq(ExhibitionBusinessOpportunity::getExhibitionId,exhibitionId)
                .set(ExhibitionBusinessOpportunity::getIsDeleted,BusinessConstant.DELETE_YES));
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public ExhibitionBusinessOpportunityVO doToVo(ExhibitionBusinessOpportunity model) {
        ExhibitionBusinessOpportunityVO modelVo = new ExhibitionBusinessOpportunityVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<ExhibitionBusinessOpportunityVO> doToVos(List<ExhibitionBusinessOpportunity> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<ExhibitionBusinessOpportunityVO> result = new ArrayList<>();
        for (ExhibitionBusinessOpportunity model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
